package com.mogist.hqc.module.main.adapter

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mogist.hqc.R
import com.mogist.hqc.entitys.response.MainVo
import talex.zsw.basecore.util.DataTool
import talex.zsw.basecore.util.DimenTool
import talex.zsw.basecore.util.MathTool
import talex.zsw.basecore.util.RegTool
import talex.zsw.basecore.util.glide.GlideApp
import talex.zsw.basecore.util.glide.GlideCircleTransform

/**
 * 作用：订单列表
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class MainAdapter(data: ArrayList<MainVo>) : BaseMultiItemQuickAdapter<MainVo, BaseViewHolder>(data)
{
    init
    { // 0:线路 1:微社区 2:产品
        addItemType(0, R.layout.item_main0)
        addItemType(1, R.layout.item_main1)
        addItemType(2, R.layout.item_main2)
    }

    override fun convert(helper: BaseViewHolder, item: MainVo)
    {
        when (helper.itemViewType)
        {
            0 -> //线路
            { // iIvImage iIvPlay iTvTitle iTvInfo iIcon iTvName iTvRead iTvComment
                var string = ""
                if (RegTool.isNotEmpty(item.route.context))
                {
                    string += item.route.context
                }
                if (RegTool.isNotEmpty(item.route.tag))
                {
                    if (RegTool.isNotEmpty(string))
                    {
                        string += "\n"
                    }
                    string += item.route.tag
                }

                helper.setText(R.id.iTvTitle, item.route.proName)
                        .setText(R.id.iTvInfo, string)
                        .setText(R.id.iTvRead, item.route.videoNum)
                        .setText(R.id.iTvComment, item.route.sayNum)
                        .setVisible(R.id.iIvPlay, RegTool.isNotEmpty(item.route.proVideo))
                        .addOnClickListener(R.id.layout0)
                loadImg(helper.getView(R.id.iIvImage),item.route.posterImg)
            }
            1 -> // 微社区
            {
                // i1LL0 i1TvTitle0 i1Image0 i1Photo0 i1TvName0 i1TvDate0 i1TvRead0 i1TvComment0
                // i1LL1 i1TvTitle1 i1Image1 i1Photo1 i1TvName1 i1TvDate1 i1TvRead1 i1TvComment1
                // i1LL2 i1TvTitle2 i1Image2 i1Photo2 i1TvName2 i1TvDate2 i1TvRead2 i1TvComment2
                if (item.community.total > 0)
                {
                    helper.setGone(R.id.i1LL0, true)
                            .setText(R.id.i1TvTitle0, item.community.list[0].title)
                            .setText(R.id.i1TvName0,
                                     DataTool.formatPhone(item.community.list[0].userName))
                            .setText(R.id.i1TvRead0, item.community.list[0].viewNum)
                            .setText(R.id.i1TvComment0, item.community.list[0].sayNum)
                    //                    if (item.community.list[0].userPostingImgList != null && item.community.list[0].userPostingImgList.isNotEmpty())
                    //                    {
                    //                        helper.setGone(R.id.i1Image0, true)
                    //                        GlideTool.loadImgRoundedCorners(helper.getView(R.id.i1Image0),
                    //                                                        item.community.list[0].userPostingImgList.get(
                    //                                                                0))
                    //                    }
                    GlideApp.with(mContext)
                            .load(item.community.list[0].userHead)
                            .centerCrop()
                            .into(helper.getView(R.id.i1Photo0))
                }
                if (item.community.total > 1)
                {
                    helper.setGone(R.id.i1LL1, true)
                            .setText(R.id.i1TvTitle1, item.community.list[1].title)
                            .setText(R.id.i1TvName1,
                                     DataTool.formatPhone(item.community.list[1].userName))
                            .setText(R.id.i1TvRead1, item.community.list[1].viewNum)
                            .setText(R.id.i1TvComment1, item.community.list[1].sayNum)
                    //                    if (item.community.list[1].userPostingImgList != null && item.community.list[1].userPostingImgList.isNotEmpty())
                    //                    {
                    //                        helper.setGone(R.id.i1Image1, true)
                    //                        GlideTool.loadImgRoundedCorners(helper.getView(R.id.i1Image1),
                    //                                                        item.community.list[1].userPostingImgList[0])
                    //                    }
                    GlideApp.with(mContext)
                            .load(item.community.list[1].userHead)
                            .centerCrop()
                            .into(helper.getView(R.id.i1Photo1))
                }
                if (item.community.total > 2)
                {
                    helper.setGone(R.id.i1LL2, true)
                            .setText(R.id.i1TvTitle2, item.community.list[2].title)
                            .setText(R.id.i1TvName2,
                                     DataTool.formatPhone(item.community.list[2].userName))
                            .setText(R.id.i1TvRead2, item.community.list[2].viewNum)
                            .setText(R.id.i1TvComment2, item.community.list[2].sayNum)
                    //                    if (item.community.list[2].userPostingImgList != null && item.community.list[2].userPostingImgList.isNotEmpty())
                    //                    {
                    //                        helper.setGone(R.id.i1Image2, true)
                    //                        GlideTool.loadImgRoundedCorners(helper.getView(R.id.i1Image2),
                    //                                                        item.community.list[2].userPostingImgList[0])
                    //                    }
                    GlideApp.with(mContext)
                            .load(item.community.list[2].userHead)
                            .centerCrop()
                            .into(helper.getView(R.id.i1Photo2))
                }

                helper.addOnClickListener(R.id.i1LL0)
                        .addOnClickListener(R.id.i1LL1)
                        .addOnClickListener(R.id.i1LL2)
                        .addOnClickListener(R.id.iTvGo)
            }
            2 -> // 产品
            { // iImage iTvTitle iTvTag1 iTvTag2 iTvTag3 iTvPrice iTvStart
                helper.setText(R.id.iTvTitle, item.product.pdtName)
                        .setText(R.id.iTvPrice, MathTool.roundStr(item.product.price, 2))
                        .setText(R.id.iTvStart, "${item.product.basePlace}出发")
                        .addOnClickListener(R.id.layout2)
                if (RegTool.isNotEmpty(item.product.tags))
                {
                    val strs =
                            item.product.tags.split("[;,/，、|]".toRegex())
                                    .dropLastWhile { it.isEmpty() }
                                    .toTypedArray()
                    helper.getView<LinearLayout>(R.id.iLLTag)
                            .visibility = View.VISIBLE
                    helper.setText(R.id.iTvTag1, strs[0])
                    if (strs.size > 1)
                    {
                        helper.setText(R.id.iTvTag2, strs[1])
                                .setVisible(R.id.iTvTag2, true)
                        if (strs.size > 2)
                        {
                            helper.setText(R.id.iTvTag3, strs[2])
                                    .setVisible(R.id.iTvTag3, true)
                        }
                        else
                        {
                            helper.setVisible(R.id.iTvTag3, false)
                        }
                    }
                    else
                    {
                        helper.setVisible(R.id.iTvTag2, false)
                                .setVisible(R.id.iTvTag3, false)
                    }
                }
                else
                {
                    helper.getView<LinearLayout>(R.id.iLLTag)
                            .visibility = View.GONE
                }

                loadImg(helper.getView(R.id.iImage), item.product.pdtImg)
            }
        }
    }

    private fun loadImg(v: ImageView, url: String)
    {
        val transform =
                GlideCircleTransform(v.context, Color.TRANSPARENT, DimenTool.dp2px(12f).toFloat())
        transform.setExceptCorner(false, false, true, true)
        GlideApp.with(v.context)
                .asBitmap()
                .load(url)
                .placeholder(R.drawable.bg_gray_c12)
                .error(R.drawable.bg_gray_c12)
                .fallback(R.drawable.bg_gray_c12)
                .transition(BitmapTransitionOptions.withCrossFade())
                .transform(transform)
                .into(v)
    }
}